/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { expect } from '@jest/globals';
import * as fse from 'fs-extra';
import * as path from 'path';

import { initByStageEmptyApp, initByStageEmptyService, initExecuteCommand, runHvigor } from './common/common';
import { ModuleBuildProfileModifier } from './common/modifier/module-build-profile-modifier';

describe('GeneratePackRes test', () => {
  test.concurrent('test show in service center stage app GeneratePackRes', async () => {
    await initByStageEmptyApp(async (projectRoot) => {
      const commands = initExecuteCommand({
        mode: 'project',
        product: 'default',
        tasks: 'GeneratePackRes'
      });
      const result = runHvigor(projectRoot, commands);
      expect(result.stdout.includes('Finished ::GeneratePackRes')).toBeTruthy();
      expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
      const result2 = runHvigor(projectRoot, commands);
      expect(result2.stdout.includes('UP-TO-DATE ::GeneratePackRes')).toBeTruthy();
      expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
      const output = path.resolve(projectRoot, 'build/outputs');
      fse.removeSync(output);
      const result3 = runHvigor(projectRoot, commands);
      expect(result3.stdout.includes('Finished ::GeneratePackRes')).toBeTruthy();
      expect(result3.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
    });
  });

  test.concurrent('test show in service center stage service GeneratePackRes', async () => {
    await initByStageEmptyService(async (projectRoot) => {
      new ModuleBuildProfileModifier(projectRoot, 'entry').updateTargetRuntimeOS('default', 'OpenHarmony');
      new ModuleBuildProfileModifier(projectRoot, 'feature').updateTargetRuntimeOS('default', 'OpenHarmony');
      new ModuleBuildProfileModifier(projectRoot, 'slibrary').updateTargetRuntimeOS('default', 'OpenHarmony');

      const commands = initExecuteCommand({
        mode: 'project',
        product: 'default',
        tasks: 'GeneratePackRes'
      });

      // openHarmony stage model不支持元服务
      const result = runHvigor(projectRoot, commands);
      expect(result.stderr.includes('Atomic service development is not supported in the OpenHarmony stage model.'))
        .toBeTruthy();
      new ModuleBuildProfileModifier(projectRoot, 'entry').updateTargetRuntimeOS('default', 'HarmonyOS');
      new ModuleBuildProfileModifier(projectRoot, 'feature').updateTargetRuntimeOS('default', 'HarmonyOS');
      new ModuleBuildProfileModifier(projectRoot, 'slibrary').updateTargetRuntimeOS('default', 'HarmonyOS');

      const modifyRuntimeOSResult = runHvigor(projectRoot, commands);
      expect(modifyRuntimeOSResult.stdout.includes('Finished ::GeneratePackRes')).toBeTruthy();

      // 增量校验
      const incrementalResult = runHvigor(projectRoot, commands);
      expect(incrementalResult.stdout.includes('UP-TO-DATE ::GeneratePackRes')).toBeTruthy();
      expect(incrementalResult.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
      const output = path.resolve(projectRoot, 'build/outputs/default/pack.res');
      fse.removeSync(output);
      const incrementalResult2 = runHvigor(projectRoot, commands);
      expect(incrementalResult2.stdout.includes('Finished ::GeneratePackRes')).toBeTruthy();
      expect(incrementalResult2.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
    }, { sign: false });
  });
});
